// import { queryProjectNotice } from '@/services/api';
import router from 'umi/router';
import {
    getMyAppList,
    getAllAppName,
    myInfo,
    login,
    logout,
    platformInfo
} from '@/services/project';
import { routerRedux } from 'dva';
import { MD5 } from 'crypto-js';

export default {
    namespace: 'project',

    state: {
        platformInfo: {},
        appList: [],
        myInfo: {},
        allAppName: [],
    },

    effects: {
        *userLogin({ payload, callback }, { call, put }) {
            let param = {
                username: payload.username,
                password: MD5(payload.password).toString(),
                appId: 3,
            }

            try {

                const response = yield call(login, param);
    
                if (response && callback) {
                    callback(response);
                }
    
                if (response.code === 0) {
                    const storage = window.sessionStorage;
                    const { data } = response;
                    storage.setItem('auth', data.auth);
                    storage.setItem('appId', '3');
                    // let power = JSON.stringify(data.power);
                    // storage.setItem('power', power);
                    yield put(
                        routerRedux.push({
                            pathname: '/',
                        })
                    );
                }
            } catch (e) {
                callback(e)
            };
        },
        *logout(_, { call, put }) {
            const response = yield call(logout);
            yield put({
                type: 'changeLoginStatus',
                payload: { status: 0, auth: '', username: '', realname: '' },
            });
            if (response.code === 0) {
                window.sessionStorage.removeItem("auth");
                window.sessionStorage.removeItem("appId");
                router.push("/login")
            }
        },
        *fetchAppList(_, { call, put }) {
            const response = yield call(getMyAppList);
            yield put({
                type: 'saveAppList',
                payload: response,
            });
        },
        *fetchMyInfo({ callback }, { call, put }) {
            const response = yield call(myInfo);
            yield put({
                type: 'saveMyInfo',
                payload: response,
            });
            if (response && callback) {
                callback(response);
            }
        },
        *fetchAllAppName({ callback }, { call, put }) {
            const response = yield call(getAllAppName);
            yield put({
                type: 'saveAllAppName',
                payload: response,
            });
            if (response && callback) {
                callback(response);
            }
        },
        *fetchPlatformInfo({ callback }, { call, put }) {
            const response = yield call(platformInfo);
            yield put({
                type: 'savePlatformInfo',
                payload: response,
            });

            if (response && callback) {
                callback(response);
            }
        },
    },

    reducers: {
        changeLoginStatus(state, { payload }) {
            const { status, auth, username, realname } = payload;
            return {
                ...state,
                status,
                auth,
                username,
                realname,
                // status: payload.status,
                // type: payload.type,
            };
        },
        saveAppList(state, action) {
            return {
                ...state,
                appList: action.payload.data
            }
        },
        saveMyInfo(state, action) {
            return {
                ...state,
                myInfo: action.payload.data
            }
        },
        saveAllAppName(state, action) {
            return {
                ...state,
                allAppName: action.payload.data
            }
        },
        savePlatformInfo(state, action) {
            return {
                ...state,
                platformInfo: action.payload.data
            }
        },
    },
};
